package cn.wolfcode.luowowo.website_api.controller;


import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.redis.redisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * 登录控制器
 */
@RestController
@Api(value = "用户资源",description = "用户资源控制器")
public class LoginController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Value("${sms.url}")
    private String url;
    @Value("${sms.appkey}")
    private String appkey;

    /**
     * 查一个用户
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    /**
     * 检查手机号码是否已注册
     *
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    /**
     * 用户注册
     * <p>
     * 请问要不要做前端注册时的验证操作??
     * 答案: 必须要 因为代码需要健壮
     */

    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "昵称",name = "nickName",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
            @ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
            @ApiImplicitParam(value = "手机号码",name = "phone",dataType = "String",required = true),
            @ApiImplicitParam(value = "短信验证码",name = "verifyCode",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="用户注册成功"),
            @ApiResponse(code=500,message="系统异常,请联系管理员"),
            @ApiResponse(code=401,message="请先登录"),
            @ApiResponse(code=501,message="自定义异常,含有异常信息返回给用户")
    })
    @PostMapping("/userRegist")
    public Object userRegist(String phone, String nickname,
                             String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone, url, appkey);
        return JsonResult.success();
    }

    /**
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/userLogin")
    public Object userLogin(String username, String password) {
        //1.用户登录操作
        UserInfo userInfo = userInfoService.userLogin(username, password);

        //2.创建token 登录令牌
        String token = UUID.randomUUID().toString().replace("-", "");

        //3.使用token作为key 用户对象作为value 存入 redis中
        userInfoRedisService.setToken(token, userInfo);

        //4.将token数据 传回给客户端保存
        /**
         * {   code:xxx
         *     msg:xxx
         *     data:{
         *        token:xxx
         *        user:xxx  }
         * }
         */
        HashMap<Object, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userInfo);
        return JsonResult.success(map);

    }

    @RequireLogin
    @GetMapping("/currentUser")
    @ApiIgnore
    public Object currentUser(HttpServletRequest request, @UserParam UserInfo userInfo) {
        /*
         *获取当前登录用户:
         *  如果已经登录过了,可以从缓存中获取登录用户信息
         *  前提: 必须拿到之前登录时候 缓存的 token key值
         *  要求: 用户访问该接口的时候 需要把token发过来
         *
         *  cookie 原则上浏览器发请求时 可以自动带上cookie信息
         *  但是这个项目是前后端分离的项目 客户端不一定是浏览器
         *  所以建议使用请求头方式 携带数据给controller
         */

        //获取请求头中信息 的 登录令牌
        /*String token = request.getHeader("token");
        //根据令牌查询 缓存中用户是否存在
        UserInfo user = userInfoRedisService.getUserByToken(token);*/
        return JsonResult.success(userInfo);
    }

}
